﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using ReallyReallyReallySimpleRoguelike.Entities;
using ReallyReallyReallySimpleRoguelike.Managers;

namespace ReallyReallyReallySimpleRoguelike.Helpers
{
    public class DungeonLevel
    {
        public IList<Wall> walls;           
        public Tile[,] Tiles;               
        public Tile[,] OldTiles;
        public Tile OpenTile;       
        public int level;

        public DungeonLevel(int level,int xMax, int yMax)
        {
            this.level = level;
            walls = new List<Wall>();
            Tiles = new Tile[xMax, yMax];
            OldTiles = new Tile[xMax, yMax];
            OpenTile = new Tile();
            BuildRandomDungeon();
        }

        public override string ToString()
        {
            return "Dungeon Level : " + level;
        }

        private void InitializeTiles()
        {
            for (int i = 0; i < Constants.DungeonHeight; i++)
            {
                for (int j = 0; j < Constants.DungeonWidth; j++)
                {
                    Tiles[j, i] = new Tile(i, j);
                }
            } 
            
            
        }

        private void BuildRandomDungeon()
        {
            InitializeTiles();
            OldTiles = (Tile[,])Tiles.Clone();
            for (int i = 0; i < Constants.DungeonWidth; i++)
            {
                Wall top = new Wall(i, 0);
                walls.Add(top);
                Wall bottom = new Wall(i, Constants.DungeonHeight - 1);
                walls.Add(bottom);
            }

            for (int i = 0; i < Constants.DungeonHeight; i++)
            {
                Wall left = new Wall(0, i);
                walls.Add(left);
                Wall right = new Wall(Constants.DungeonWidth - 1, i);
                walls.Add(right);
            }
        }

        //Refactor 9: IsInvalidValidMove has been renamed to IsOKToMove
        //Refactor 10:less than greater than logic to determine a valid move
        public bool IsOKToMove(Point p)
        {
            return ((p.X > 0 && p.X < Constants.DungeonWidth - 1) && (p.Y < Constants.DungeonHeight - 1 && p.Y > 0));
        }


        public static void ClampToDungeon(Point p)
        {

            p.X = Math.Max(Math.Min(p.X, Constants.DungeonWidth),0);
            p.Y = Math.Max(Math.Min(p.Y, Constants.DungeonHeight),0);

        }

        internal void AddTile(Tile t)
        {
            Tiles[t.X, t.Y] = t;
        }

        internal Tile GetTile(Point point)
        {
            return Tiles[point.X, point.Y];
        }

        internal void Clear()
        {
            InitializeTiles();

            //for (int i = 0; i < Constants.DungeonHeight; i++)
            //{
            //    for (int j = 0; j < Constants.DungeonWidth; j++)
            //    {
            //        //hack to stop redraw of blank tiles
            //        if (Tiles[j, i].ImageCharacter != Constants.TileImage)
            //            Tiles[j, i].Initialize(j, i);
            //    }
            //}
        }

        internal void AddStairwell(Stairwell stairwell)
        {
            Tiles[stairwell.X, stairwell.Y] = stairwell;
        }

        public void CreateRandomDungeonObjects()
        {
            DungeonManager.Instance.CreatureManager.ClearMonsters();
            DungeonManager.Instance.CreatureManager.AddMonsters();

            DungeonManager.Instance.PropManager.ClearProps();
            DungeonManager.Instance.PropManager.AddProps();
        }

        public Point GetValidRandomPoint()
        {
            Point p;
            do
            {
                p = new Point(DungeonManager.Instance.random.Next(Constants.DungeonWidth), DungeonManager.Instance.random.Next(Constants.DungeonWidth));
            }
            while (!IsOKToMove(p));
            return p;
        }
    }
}
